package com.kaikeba.wx.controller;

import com.kaikeba.bean.Courier;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.User;
import com.kaikeba.bean.WxUser;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierService;
import com.kaikeba.service.UserService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.RandomUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {
    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        //String code = RandomUtil.getCode()+"";
        //boolean flag = SMSUtil.loginSMS(userPhone, code);
        String code = "123456";
        boolean flag = true;
        Message msg = new Message();
        if(flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送,请查收!");
        }else{
            //短信发送失败
            msg.setStatus(1);
            msg.setResult("验证码下发失败,请检查手机号或稍后再试");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);

        String json = JSONUtil.toJSON(msg);
        return json;
    }

     // 用户登入 若无用户信息则注册用户 若存在用户信息则更新手机号
    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();
        if(sysCode == null){
            //这个手机号未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        }else if(sysCode.equals(userCode)){
            //这里手机号码和短信一致 , 登陆成功
            //TODO 这个判断应替换为快递员表格查询手机号的结果
            //1.判断快递员表格是否有该手机号 有返回true
            Courier c = CourierService.findByUserPhone(userPhone);
            WxUser wxUser = new WxUser();
            if(c!=null){
                //快递员
                msg.setStatus(1);
                wxUser.setUser(false);
            }else{
                //用户
                //TODO 判断用户表是否存在，若不存在则插入
                msg.setStatus(0);
                wxUser.setUser(true);
            }
            //2.判断用户表是否存在该手机号 若不存在，则插入注册信息，存在则更新登入时间
            User u = UserService.findByUserPhone(userPhone);
            if (u==null){
                User user = new User(null, userPhone, "123");
                UserService.insert(user);
            }else{
                //更新登入时间
                UserService.updateLoginTime(userPhone);
            }

            wxUser.setUserPhone(userPhone);
            UserUtil.setWxUser(request.getSession(),wxUser);
        }else{
            //这里是验证码不一致 , 登陆失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        WxUser user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if(isUser)
            msg.setStatus(0);
        else
            msg.setStatus(1);
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    //根据手机号返回用户详细信息
    @ResponseBody("/wx/userDetails.do")
    public String userDetails(HttpServletRequest request,HttpServletResponse response){
        WxUser user = UserUtil.getWxUser(request.getSession());
        String userPhone = user.getUserPhone();
        User u = UserService.findByUserPhone(userPhone);
        Message msg = new Message();
        if (u==null){
            msg.setStatus(-1);
            msg.setResult("用户不存在，请重新登入");
        }
        else{
            msg.setStatus(0);
            msg.setResult("用户查询成功");
        }
        //判断是否有昵称
        if (u.getNickname()==null||u.getNickname().length()==0){
            u.setNickname("用户"+u.getId());
        }
        msg.setData(u);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    //根据手机号修改用户信息
    @ResponseBody("/wx/modifyUserInfo.do")
    public String modifyUserInfo(HttpServletRequest request,HttpServletResponse response){
        String userPhoneNew = request.getParameter("userPhone");
        String nickname = request.getParameter("username");
        WxUser user = UserUtil.getWxUser(request.getSession());
        String userPhone = user.getUserPhone();
        User newUser = new User(nickname, userPhoneNew,"123");
        boolean update = UserService.update(userPhone,newUser);
        Message msg = new Message();
        if (update==false){
            msg.setStatus(-1);
            msg.setResult("用户更新失败");
        }
        else{
            msg.setStatus(0);
            msg.setResult("用户更新成功");
        }
        //判断手机号是否更新 若更新则更改session
        if (!userPhone.equals(userPhoneNew)){
            WxUser wxUserNew = new WxUser(userPhoneNew, user.isUser());
            UserUtil.setWxUser(request.getSession(),wxUserNew);
        }

        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //1.    销毁session
        request.getSession().invalidate();
        //2.    给客户端回复消息
        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }
}
